package com.hotel.controller;

import com.hotel.bean.Employee;
import com.hotel.bean.RespBean;
import com.hotel.exception.SteduException;
import com.hotel.service.DeptService;
import com.hotel.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/admin/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private DeptService deptService;
    //添加
    @PostMapping
    public RespBean insert(@RequestBody  @Validated Employee employee){
        if (employeeService.insert(employee)) {
            return RespBean.ok("添加成功");
        }
        return RespBean.error("添加失败,请联系管理员");
    }
    //根据eid删除
    @DeleteMapping("/{eid}")
    public RespBean deleteByEid(@PathVariable Integer eid) throws SteduException {
        if (employeeService.deleteByEid(eid)) {
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败,请联系管理员");
    }
    //根据eid修改
    @PutMapping
    public RespBean update(@RequestBody  @Validated Employee employee) throws SteduException {
        if (employeeService.update(employee)) {
            return RespBean.ok("修改成功");
        }
        return RespBean.error("修改失败，请联系管理员");
    }
    //根据eid查询
    @GetMapping("/{eid}")
    public RespBean selectByEid(@PathVariable Integer eid) throws SteduException {
        if(employeeService.selectByEid(eid) != null){
            return RespBean.ok("",employeeService.selectByEid(eid));
        }
        return RespBean.error("出错了。请联系管理员");
    }
    //查询所有
    @GetMapping
    public RespBean selectAll(String ename){
        List<Employee> employees = employeeService.selectAll(ename);
        return RespBean.ok("",employees);
    }
    //获取所有部门信息
    @GetMapping("/allDept/{eid}")
    public RespBean allDept(@PathVariable("eid") Integer eid){
        List<Map<String, Object>> deptMapList = deptService.selectAll()
                .stream()
                .map(e -> {
                    Map<String, Object> deptMap = new HashMap<>();
                    deptMap.put("key", e.getDid());
                    deptMap.put("label", e.getDname());
                    return deptMap;
                }).collect(Collectors.toList());
        //已经被分配的部门
        List<Integer> dids = employeeService.selectDidByEid(eid);
        Map<String, Object> map = new HashMap<>();
        map.put("allDeptList",deptMapList);
        map.put("selectedDids",dids);

        return RespBean.ok("",map);
    }
    @PostMapping("/eidAndDid")
    public RespBean insertEidAndDid(@RequestBody Map<String,Object>map) throws SteduException {
        Integer eid = (Integer) map.get("eid");
        List<Integer> didsList = (List<Integer>) map.get("dids");
        Integer[] dids = new Integer[didsList.size()];
        for (int i = 0; i < didsList.size(); i++) {
            int did = didsList.get(i);
            dids[i] = new Integer(did + "");
        }
        employeeService.insertEidAndDid(eid,dids);

        return RespBean.ok("分配部门成功");
    }
}
